<?
	require_once('config.php');
 	require_once('functions/general.php');
	require_once('functions/sql.php');
	require_once('sql/sql_incident.php');
	require_once('sql/sql_discussion.php');
	
	$mysql_link = get_mysql_link();

	class RSS extends DomDocument {
		private $channel;
		function __construct($link, $description, $title) {
			parent :: __construct('1.0');
			$rss = $this->createElement('rss');
			$rss->setAttribute('version', '2.0');
			$this->appendChild($rss);
	
			$this->channel = $this->createElement('channel');
			$this->channel->appendChild(
				$this->create_text_node('title', $title));
			$this->channel->appendChild(
				$this->create_text_node('link', $link));
			$this->channel->appendChild(
				$this->create_text_node('description', $description));
	
			//puts channel nested under rss
			$rss->appendChild($this->channel);
	
			$this->formatOutput = true;
			
		}
		/**
		* Add new item under the channel
		*/
	
		public function new_item($title, $description, $link, $guid = null) {
			$item = $this->createElement('item');
			$item->appendChild(
				$this->create_text_node('title', $title));
			$item->appendChild(
				$this->create_text_node('link', $link));
			$item->appendChild(
				$this->create_text_node('description', $description));
			if ($guid) {
				$item->appendChild(
					$this->create_text_node('guid', $guid));
			}
			$this->channel->appendChild($item);
	
		}
	
		private function create_text_node($title, $text) {
			$element = $this->createElement($title);
			$element->appendChild($this->createTextNode($text));
			return $element;
		}
	}
	
	
	if (!isset($_GET['feed']) or $_GET['feed'] == 'latest') {
		//Display the latest offenses submitted
		header("Content-Type: application/rss+xml;charset=UTF-8");

		$rss = new RSS("$CONFIG[url]", "Siste $CONFIG[rss_latest] hendelser", 'Den Hellige bilist - Siste 10 hendelser');
		$incident = new Sql_incident($mysql_link);
		$data = $incident->get_last($CONFIG[rss_latest]);
		
		foreach($data as $i) {
			$description = "
				<h2>$i[offender] - $i[offense]</h2>
				<strong>Område</strong>: $i[area]<br />
				<strong>Bilnummer</strong>: <a href=\"$CONFIG[url]/browse.php?category=offender&amp;selection=$i[offender]\">					$i[offender]</a><br />
				<strong>Tidspunkt</strong>: $i[date] $i[time]<br />
				<strong>Synd</strong>: $i[offense]<br />
				<strong>Kommentar</strong>: $i[comment] <br />
				<strong>Innsent av</strong>: <a href=\"$CONFIG[url]/profile.php?user=$i[user]\">$i[user]</a>
			";
	
			$rss->new_item(
				"$i[offender]: $i[offense]",
				$description,
				"$CONFIG[url]/view.php?incident=$i[id]",
				"$CONFIG[url]/view.php?incident=$i[id]"
			);
		}

		print $rss->saveXML();

	} 

	else if (isset($_GET['feed']) and $_GET['feed'] == 'discussion') {
		//Display the latest discussion threads
		header("Content-Type: application/rss+xml;charset=UTF-8");
		$rss = new RSS("$CONFIG[url]/discuss.php", "Diskusjon", "Den Hellige bilist - Diskusjon");
		$discussion = new Sql_discussion($mysql_link);
		$data = $discussion->get_threads($CONFIG['discussion_shown']);
		foreach($data as $thread) {
			
			//put the first post, and the last reply to the description
			$posts = $discussion->get_posts($thread[id], 2);
			
			$first_post = $posts[0];
			$last_reply = $posts[1];


			$description = "
				<h2>$thread[title]</h2>
				<h3>Første innlegg</h3>
				$first_post[text]<br />
				<small>Innsent av $first_post[user]</small>";

			if ($last_reply) {
				$description .= "
					<h3>Siste innlegg</h3>
					$last_reply[text]<br />
					<small>Innsent av $last_reply[user]</small>";
			}
			else
				$description .= "<h3>Ingen har svart på denne tråden</h3>";

			$replies = implode($discussion->thread_replies($thread[id]));
			$rss->new_item(
				"$thread[title] ($replies)",
				$description,
				"$CONFIG[url]/discussion.php?thread=$thread[id]",
				"$CONFIG[url]/discussion.php?thread=$thread[id]"
			);
				
				
			
		}
		
		print $rss->saveXML();	
	}

	else {
		print "No feed or invalid feed requested.";
	}
	

?>
